#include "../header/common.h"
#include <time.h>
#include <SDL/SDL_ttf.h>





void online(SDL_Surface *screen,int mode,char name[50])
{ 
  SDL_Surface *back, *button[6],*temp;
  
  SDL_Event event;
  SDL_Rect rect[4];  
  int i,done=0,click=0;
  SDL_MouseMotionEvent mm;
  SDL_MouseButtonEvent mb;
  /* bilder und auflösungsabhängigewerte werden geladen */
  switch(screen->w)
    {
    case 640:
      back=SDL_LoadBMP("image/640480/SvsC.bmp");   
      button[0]=SDL_LoadBMP("image/640480/buttons/serverstarten1.bmp");
      button[3]=SDL_LoadBMP("image/640480/buttonspressed/serverstarten2.bmp");
      button[1]=SDL_LoadBMP("image/640480/buttons/clientstarten1.bmp");
      button[4]=SDL_LoadBMP("image/640480/buttonspressed/clientstarten2.bmp");
      button[2]=SDL_LoadBMP("image/640480/buttons/abbrechen1.bmp");
      button[5]=SDL_LoadBMP("image/640480/buttonspressed/abbrechen2.bmp");
      temp=SDL_LoadBMP("image/640480/temp.bmp"); 
      rect[0].x=190;rect[0].y=150;rect[0].w=button[0]->w;rect[0].h=button[0]->h;
      rect[1].x=190;rect[1].y=190;rect[1].w=button[1]->w;rect[1].h=button[1]->h;
      rect[2].x=190;rect[2].y=255;rect[2].w=button[2]->w;rect[2].h=button[2]->h;
      rect[3].x=160;rect[3].y=120;
      break;
    case 800:
      back=SDL_LoadBMP("image/800600/SvsC.bmp");   
      button[0]=SDL_LoadBMP("image/800600/buttons/serverstarten1.bmp");
      button[3]=SDL_LoadBMP("image/800600/buttonspressed/serverstarten2.bmp");
      button[1]=SDL_LoadBMP("image/800600/buttons/clientstarten1.bmp");
      button[4]=SDL_LoadBMP("image/800600/buttonspressed/clientstarten2.bmp");
      button[2]=SDL_LoadBMP("image/800600/buttons/abbrechen1.bmp");
      button[5]=SDL_LoadBMP("image/800600/buttonspressed/abbrechen2.bmp");
      temp=SDL_LoadBMP("image/800600/temp.bmp"); 
     
      rect[0].x=238;rect[0].y=188;rect[0].w=button[0]->w;rect[0].h=button[0]->h;
      rect[1].x=238;rect[1].y=238;rect[1].w=button[1]->w;rect[1].h=button[1]->h;
      rect[2].x=238;rect[2].y=319;rect[2].w=button[2]->w;rect[2].h=button[2]->h;
      rect[3].x=200;rect[3].y=150;
           
      break;

    case 1024:
back=SDL_LoadBMP("image/1024768/SvsC.bmp");   
 button[0]=SDL_LoadBMP("image/1024768/buttons/serverstarten1.bmp");
      button[3]=SDL_LoadBMP("image/1024768/buttonspressed/serverstarten2.bmp");
      button[1]=SDL_LoadBMP("image/1024768/buttons/clientstarten1.bmp");
      button[4]=SDL_LoadBMP("image/1024768/buttonspressed/clientstarten2.bmp");
      button[2]=SDL_LoadBMP("image/1024768/buttons/abbrechen1.bmp");
      button[5]=SDL_LoadBMP("image/1024768/buttonspressed/abbrechen2.bmp");
      temp=SDL_LoadBMP("image/1024768/temp.bmp"); 
     
      rect[0].x=305;rect[0].y=239;rect[0].w=button[0]->w;rect[0].h=button[0]->h;
      rect[1].x=305;rect[1].y=305;rect[1].w=button[1]->w;rect[1].h=button[1]->h;
      rect[2].x=305;rect[2].y=408;rect[2].w=button[2]->w;rect[2].h=button[2]->h;
      rect[3].x=256;rect[3].y=192;    
      break;
    }

  
  /* fenster wird gezeichnet*/ 
 
  SDL_SetColorKey(back, SDL_SRCCOLORKEY, SDL_MapRGB(back->format, 0, 0, 255));
  SDL_BlitSurface(back,NULL,screen,&rect[3]);
  SDL_BlitSurface(button[0],NULL,screen,&rect[0]);
  SDL_BlitSurface(button[1],NULL,screen,&rect[1]);
  SDL_BlitSurface(button[2],NULL,screen,&rect[2]);
  
 
  /*eventschleife wird gestartet*/
  while(!done)
    {
      while (SDL_PollEvent(&event))
	{
	  switch(event.type)
	    {
	    case SDL_QUIT:
	      done = 1;
	      break;
	      
	    case SDL_KEYDOWN:
	      switch(event.key.keysym.sym)
	       {
	       case SDLK_ESCAPE:
		 done=1;
		 break;
	       }		 
		 
	      /*mousebewegungen werden ausgewertet für mousoverbutton*/
	case SDL_MOUSEMOTION:
	  mm=event.motion;	  
	   for(i=0;i<3;i++)
	    {
	      click=click_button(rect[i],mm.x,mm.y);
	     switch(click)
	      {
	      case 0:
		SDL_BlitSurface(button[i],NULL,screen,&rect[i]);
		break;
	      case 1:
		SDL_BlitSurface(button[i+3],NULL,screen,&rect[i]);
		break;
	      
	      }
	    }
	  break;  
	  /*mouseclicks werden ausgewertet und wenn nötig nächste fenster geladen */
	case SDL_MOUSEBUTTONDOWN:
	  mb=event.button;
	  for(i=0;i<3;i++)
	    {
	      click=click_button(rect[i],mb.x,mb.y);
	      if(click)
		{
		  switch(i)
		    {
		    case 0:
		      server(screen,name);
		      done=1;
		      break;
		    case 1:
		      ipeingeben(screen,name);
		      done=1;
		      break;
		      
		    case 2:
		      done=1;
		      break;
		    }
		}  
	      
	    }
	  break;
	    }
	  
	}
      SDL_Flip(screen);
      SDL_Delay(100);
    }
  /* bilder werden wieder freigegeben*/
  SDL_FreeSurface(back);
  SDL_FreeSurface(button[0]);
  SDL_FreeSurface(button[1]);
  SDL_FreeSurface(button[2]);
  SDL_FreeSurface(button[3]);
  SDL_FreeSurface(button[4]);
  SDL_FreeSurface(button[5]);
   
}

